//
//  APPLEStatusOriginalView.swift
//  123
//
//  Created by pangjiaxing on 16/1/2.
//  Copyright © 2016年 pangjiaxing. All rights reserved.
//

import UIKit
import SnapKit
class APPLEStatusOriginalView: UIView {
    
    // MARK: - 懒加载控件
    
    // 头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_small"))
    // 昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontsize: 14)
    // 时间与来源的label
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), fontsize: 10)
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontsize: 10)
    // 会员图标
    private lazy var memberView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    // 认证的图标
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    /// 内容的label
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontsize: 15)
        // 多行属性
        label.numberOfLines = 0
        return label
    }()
    
    
    
    
    
    
    /// 配图视图
    private lazy var statusPictureView: APPLEStatusPictureView = {
        let statusPictureView = APPLEStatusPictureView()
        statusPictureView.backgroundColor = self.backgroundColor
        return statusPictureView
    }()
    
    
    
    // 记录当前控件的底部约束
    var bottomConstraint: Constraint?
    
    
    
    
    var statusViewModel:APPLEStatusViewModel?{
        didSet{
            // 设置数据
            nameLabel.text = statusViewModel?.status?.user?.screen_name
            // 设置头像
            if let urlString = statusViewModel?.status?.user?.profile_image_url {
                iconView.sd_setImageWithURL(NSURL(string: urlString), placeholderImage: UIImage(named: "avatar_default_small"))
            }
            
            // 逻辑写在这儿合适吗？
            memberView.image = statusViewModel?.memberImage
            verifiedView.image = statusViewModel?.verifiedImage
            //            if let mbrank = statusViewModel?.status?.user?.mbrank where mbrank > 0 && mbrank < 7 {
            //                memberView.image = UIImage(named: "common_icon_membership_level\(mbrank)")
            //            }else{
            //                // 设置会员图标
            //                memberView.image = nil
            //            }
            
            // 设置微博内容
            contentLabel.text = statusViewModel?.status?.text
            
            // TODO: 时间与来源，先这么显示，然后后面会处理
            timeLabel.text = statusViewModel?.createText
            sourceLabel.text = statusViewModel?.sourceText
            

            
            
            
            self.bottomConstraint?.uninstall()
            
            if let picUrls = statusViewModel?.status?.pic_urls where picUrls.count > 0{
                statusPictureView.hidden = false
                statusPictureView.picUrls = picUrls
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(statusPictureView).offset(AppleStatusCellMargin).constraint
                })
            }else{
                self.snp_updateConstraints(closure: { (make) -> Void in
                    statusPictureView.hidden = true
                    self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(AppleStatusCellMargin).constraint
                })
               
            }
            
            
            
        }
    }
    override init(frame: CGRect) {
       super.init(frame: frame)
//        backgroundColor = UIColor(white: 0.95, alpha: 1)
//        backgroundColor = UIColor.redColor()
        backgroundColor = UIColor.whiteColor()
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUpUI(){
        
        // 添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        //配图视图
         addSubview(statusPictureView)
        
        
        // 添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(AppleStatusCellMargin)
            make.top.equalTo(self).offset(AppleStatusCellMargin)
            make.size.equalTo(CGSizeMake(AppleStatusIconViewH, AppleStatusIconViewH))
        }
        // 认证的图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing).offset(-3)
            make.centerY.equalTo(iconView.snp_bottom).offset(-3)
        }
        
        // 名字
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(AppleStatusCellMargin)
            make.top.equalTo(iconView)
        }
        
        // 会员
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(AppleStatusCellMargin)
            make.centerY.equalTo(nameLabel)
        }
        
        // 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(iconView)
        }
        
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(AppleStatusCellMargin)
            make.top.equalTo(timeLabel)
        }
        
        // 内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(AppleStatusCellMargin)
            // 约束宽度
            make.width.lessThanOrEqualTo(self.snp_width).offset(2 * -AppleStatusCellMargin)
        }
        
        // 配图视图
        statusPictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(AppleStatusCellMargin)
            make.leading.equalTo(contentLabel)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstraint = make.bottom.equalTo(contentLabel).offset(AppleStatusCellMargin).constraint
        }
        
        
       
        
    }
}
